package com.easy_helth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy_helth.entity.User;
import com.easy_helth.mapper.UserMapper;
import com.easy_helth.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


//    判断用户的健康情况
    @Override
    public User is_health(User user) {
        if (user.getHeight()==0 | user.getBp_d()== 0 ) {
            double BMI = user.getWeight() / user.getHeight() * user.getHeight();
            if (BMI > 23.9)  user.setHealth("2");
            if (BMI < 18.5) user.setHealth("1");
        }
       return user;
    }

    //    获取用户分布总数
    @Override
    public Map<String, Integer> getUserNumber() {
       List<String> privice = Arrays.asList("南海诸岛","北京","天津","上海","重庆","河北","河南","云南","辽宁","黑龙江","湖南"
       ,"安徽","山东","新疆","江苏","浙江","江西","湖北","广西","甘肃","山西","内蒙古","陕西","吉林","福建","贵州","广东","青海",
               "西藏","四川","宁夏","海南","台湾","香港","澳门");
       List<User> user_address = this.list(null);
       Map<String,Integer> addressMap = new TreeMap<>();
       log.info(String.valueOf(user_address));
       for (String i:privice){
//           全部设置基础数据
           addressMap.put(i,200);
       }
       for (User user: user_address) {
           if (addressMap.get(user.getAddress()) != null) {
               addressMap.put(user.getAddress(), addressMap.get(user.getAddress()) + 55);//放大十倍
           }else {
               addressMap.put(user.getAddress(),550);
           }
       }
        return addressMap;
    }

    @Override
    public Map<String, Integer> getUserAge() {
        List<String> ageList = Arrays.asList("20岁以下","20-29岁","30-39岁","40-49岁","50岁以上");
        List<User> userList = this.list(null);
        Map<String,Integer> ageMap = new TreeMap<>();
        for (String i : ageList){
//            设置20基数
            ageMap.put(i,20);
        }
        for (User user : userList){
            if (user.getAge() < 20){
                ageMap.put("20岁以下",ageMap.get("20岁以下") + 1);
            }
            else if (user.getAge() >20 && user.getAge() <30){
                    ageMap.put("20-29岁",ageMap.get("20-29岁") + 1);
                }
            else if (user.getAge() >30 && user.getAge() <40){
                ageMap.put("30-39岁",ageMap.get("30-39岁") + 1);
            }
            else if (user.getAge() >40 && user.getAge() <50){
                ageMap.put("40-49岁",ageMap.get("40-49岁") + 1);
            }
            else {
                ageMap.put("50岁以上",ageMap.get("50岁以上") + 1);
            }

        }
        return ageMap;
    }


}
